﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GrapplingHookGameData
{
    public class HealthBar:GameObject
    {
        private float targetHealth;
        public float TargetHealth {
            get { return TargetHealth; }
            set { targetHealth = MathHelper.Clamp(value, 0, 100); }
        }
        private float currentHealth;
        private Player player;

        public HealthBar(Vector2 position, Player player)
            : base(position, 250, 50, "hud/SpriteSheet", true)
        {
            this.player = player;
            currentHealth = 100.0f;
            TargetHealth = currentHealth;
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            base.LoadContent(content);
        }

        public override void Update(GameTime gameTime, bool isHost)
        {
            //update local players healthbar display
            TargetHealth = player.health;

            //animate healthbar
            if (currentHealth != targetHealth)
            {
                currentHealth += (targetHealth - currentHealth) * gameTime.ElapsedGameTime.Milliseconds * Constants.HEALTH_ANIMATE_SPEED;
                currentHealth = MathHelper.Clamp( (float)Math.Round(currentHealth), 0, 100 );
            }

            base.Update(gameTime, isHost);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Rectangle currentSprite = spriteSheet.SourceRectangle(currentFrame+1);
            currentSprite.Width = (int)(currentSprite.Width * currentHealth / 100.0f);
            currentSprite.X = (int)(currentSprite.X*currentHealth / 100.0f);
            spriteBatch.Draw(spriteSheet.Texture, new Rectangle((int)position.X, (int)position.Y, currentSprite.Width, currentSprite.Height), currentSprite, Color.White, Rotation, origin, SpriteEffects.None, 0);
            //draw border
            base.Draw(spriteBatch);
        }

    }
}
